﻿
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Warehouse.Infrstructre
{
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {


        private readonly EFCoreDBcontext db;

        public BaseRepository(EFCoreDBcontext db)
        {
            this.db = db;
        }

        public async Task<int> AddAsync(T t)
        {
            db.Set<T>().Add(t);
            return await db.SaveChangesAsync();
        }

        public async Task<List<T>> GetAllAsyns()
        {
            return await db.Set<T>().ToListAsync();
        }

        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> expre)
        {
            return await db.Set<T>().Where(expre).ToListAsync();
        }

        public async Task<T> GetModelAsync(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }

        public async Task<int> RemoveAsync(T t)
        {
            db.Set<T>().RemoveRange(t);
            return await db.SaveChangesAsync();

        }

        public async Task<int> UpdateAsync(T t)
        {
            db.Update(t);
            return await db.SaveChangesAsync();
        }
    }
}
